/****************************************************************
 * <strong>MFSQLCondition分页查询管理</strong>
 * <p>
 * MFSQLCondition分页查询管理
 * </p>
 *
 * @author AndyZhang 2011
 * @version 0.1
 * ToDo:
 ***************************************************************/

package com.thinkwide.data.ormapping.sql;

import com.thinkwide.data.ormapping.MFORMappingGlobe;
import com.thinkwide.data.ormapping.datatable.MFDataTable;

import java.math.BigDecimal;
import java.sql.SQLException;

public class MFSqlCondition {
    private Integer pageSize = -1;
    private Integer pageIndex = -1;
    private String dbName = "";
    private String sql = "";
    private Integer pageMax = -1;
    private Integer recordCount = -1;

    public MFSqlCondition() {

    }

    public MFSqlCondition(String sql, int pageSize, int pageIndex) {
        this("", sql, pageSize, pageIndex);
    }

    public MFSqlCondition(String sql) {
        this("", sql, -1, -1);
    }

    public MFSqlCondition(String dbName, String sql, int pageSize, int pageIndex) {
        this.dbName = dbName;
        this.sql = sql;
        this.pageSize = pageSize;
        this.pageIndex = pageIndex;
    }

    public void refreshPageMax() {
        recordCount = getRecordCount();
    }

    public Integer getPageMax() {
        if (pageMax == -1) {
            refreshPageMax();
        }

        float f = (float) recordCount / (float) pageSize;
        pageMax = (int) Math.ceil(f);

        return pageMax;
    }

    public int getRecordCount() {
        String sSQL = String.format(MFSqlSelect.SELECT_COUNT_FROM, sql);

        Object iCount = -1;
        try {
            iCount = MFORMappingGlobe.getDataAdapter().execScalar(sSQL);
        } catch (SQLException e) {

            e.printStackTrace();
        }

        BigDecimal i = (BigDecimal) iCount;
        return i.intValue();
    }


    public MFDataTable getPageData(int PageIndex) {
        return null;
    }

    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }

    public Integer getPageSize() {
        return pageSize;
    }

    public void setPageIndex(Integer pageIndex) {
        this.pageIndex = pageIndex;
    }

    public Integer getPageIndex() {
        return pageIndex;
    }

    public void setDbName(String dbName) {
        this.dbName = dbName;
    }

    public String getDbName() {
        return dbName;
    }

    public void setSql(String sql) {
        this.sql = sql;
    }

    public String getSql() {
        return sql;
    }


}
